import static java.lang.StrictMath.PI;
import static java.lang.StrictMath.abs;
import model.ActionType;
import model.Game;
import model.Hockeyist;
import model.HockeyistState;
import model.Move;
import model.Unit;
import model.World;

/**
 *
 * @author Constantine Tretyak
 */
public class PickUp extends AbstractStrategy {

    public PickUp(Hockeyist self, World world, Game game, Move move) {
        super(self, world, game, move);
    }

    @Override
    public void run() {

        if (self.getState() == HockeyistState.SWINGING) {   // если я остался в замахе
            move.setAction(ActionType.CANCEL_STRIKE);
            return;
        }

        if (BACK_AFTER_SHOOT_TICKS > 0 && selfX < 0.0d) {
            move.setSpeedUp(1.0d);
            move.setTurn(self.getAngleTo(getNet(true)));
            return;
        }

        if (AGGRESSIVE_ATTACK_OPP_TICKS > 0) {
            moving.moveTo(puck, null);
            if (self.getDistanceTo(puck) < game.getStickLength() && moving.atSector(self, puck, game.getStickSector())) {
                move.setAction(ActionType.TAKE_PUCK);
            }

            if (oppBlockUnit(self)) {
                move.setAction(ActionType.STRIKE);
                move.setSpeedUp(1.0d);
            }
//
            if (oppFrontMeWithPuck(self) && self.getDistanceTo(puck) > game.getStickLength()) {
                move.setAction(ActionType.STRIKE);
                move.setSpeedUp(1.0d);
            }
            return;
        }

        move.setAction(ActionType.TAKE_PUCK);

        if (self.getId() == nearHockiyst(puck).getId()) {   // Если я ближе к шайбе
            if (puckX < selfX && self.getDistanceTo(puck) > coords.getWidth() / 2) {    // если шайба дальше от меня на 1/2 поля
                moving.moveTo(new Point(50, coords.getXcoord(0.3d), coords.getYcoord(Math.signum(puckY) * 0.3d)), puck);
            } else {
                if (READY_TO_PASS_TICKS > 0) {
                    move.setTurn(self.getAngleTo(puck));
                    if (abs(self.getAngleTo(puck)) < 0.5 * PI / 180) {
                        move.setSpeedUp(-1.0d);
                    }
                } else {
                    moving.moveTo(puck, null);
                }
            }
        } else {    //   если я дальше от щайбы
            defendNet(false);
        }
        if (self.getDistanceTo(puck) < game.getStickLength() && moving.atSector(self, puck, game.getStickSector()) && !getOppsFront(self, game.getStickLength()).isEmpty()) {
            move.setAction(ActionType.STRIKE);
        }

        if (oppBlockUnit(self)) {
            move.setAction(ActionType.STRIKE);
            move.setSpeedUp(1.0d);
        }
//
        if (oppFrontMeWithPuck(self) && self.getDistanceTo(puck) > game.getStickLength()) {
            move.setAction(ActionType.STRIKE);
            move.setSpeedUp(1.0d);
        }

        if (self.getDistanceTo(puck) > game.getStickLength() && moving.atSector(self, puck, 2 * PI / 180) && moving.getSpeed(puck) > 10d && selfX > 0.7d && abs(selfY) < 0.4d) {
            move.setSpeedUp(-1.0d);
            return;
        }

        double turnTicks = 0;
        Unit selfCopy = moving.cast(self, puck, 0.0d, 0);
        Unit puckCopy = moving.cast(puck, null, 0.0d, 0);
        while (turnTicks <= abs(self.getAngleTo(puck)) / game.getHockeyistTurnAngleFactor() + 3) {
            if (selfCopy.getDistanceTo(puckCopy) < game.getStickLength() && moving.atSector(selfCopy, puckCopy, game.getStickSector())) {
                move.setTurn(self.getAngleTo(puckCopy));
                return;
            }

            selfCopy = moving.cast(selfCopy, puckCopy, 0.0d, 1);
            puckCopy = moving.cast(puckCopy, null, 0.0d, 1);
            turnTicks++;
        }

        if (self.getDistanceTo(puck) < game.getStickLength() && moving.atSector(self, puck, game.getStickSector())
                && getZone(self) < 3d
                && abs(moving.getSpeed(puck)) > 10) {
            if (self.getDistanceTo(getOpponents(self).get(0)) > coords.getWidth() / 3) {
                move.setAction(ActionType.TAKE_PUCK);
            } else {
                move.setAction(ActionType.STRIKE);
            }
        }

    }

}
